package com.book.xsp.bookparent.controller;


import com.book.xsp.bookparent.config.R;
import com.book.xsp.bookparent.entity.Course;
import com.book.xsp.bookparent.entity.vo.AddCourseVo;
import com.book.xsp.bookparent.entity.vo.EditCourseVo;
import com.book.xsp.bookparent.service.ICourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author snake_skin
 * @since 2023-11-28
 */
@RestController
@RequestMapping("/course")
@CrossOrigin
@Slf4j
public class CourseController {

    @Resource
    ICourseService iCourseService;

    // 返回全部课程数据
    @GetMapping
    public R getAllCourse() {
        List<Course> list = iCourseService.getAllCourse();
        return R.ok().data("list",list);
    }

    // 新增课程
    @PostMapping
    public R addCourse(@RequestBody AddCourseVo course) {
        boolean b = iCourseService.addCourse(course);
        return R.ok().success(b);
    }


    // 编辑课程
    @PutMapping
    public R editCourse(@RequestBody EditCourseVo course) {
        log.info("编辑课程信息：{}",course);
        boolean b = iCourseService.editCourse(course);
        return R.ok().success(b);
    }

    @DeleteMapping("/{id}")
    public R deleteCourse(@PathVariable Long id) {

        boolean b = iCourseService.deleteCourse(id);
        return R.ok().success(true);
    }
}
